package com.itic.codeGen.codeGenerator.workLine.implement;

import java.util.ArrayList;
import java.util.List;

import com.itic.codeGen.codeGenerator.util.CommonUtils;
import com.itic.codeGen.codeGenerator.util.CommonUtils.FILE_TYPE;
import com.itic.codeGen.codeGenerator.workLine.PartGenerator;

public class ServiceGenerator implements PartGenerator {

    private static final String AUTOWIRE = "org.springframework.beans.factory.annotation.Autowired";
    private static final String SERVICE = "org.springframework.stereotype.Service";
    private static final String TRANSACTIONAL = "org.springframework.transaction.annotation.Transactional";
    private static final String CrudService = "com.itic.appbase.framework.common.service.CrudService";
    private static final String Annotation = "@Service";
    private static final String Annotation_1 = "@Transactional";
    private static final String Annotation_2 = "@Autowired";

    private String packageName;
    private String domainName;
    private ArrayList<String> imports = new ArrayList<String>();
    private ArrayList<String> headers = new ArrayList<String>();
    private String extend;
    private ArrayList<String> body = new ArrayList<String>();

    public ServiceGenerator(String packageName, String domainName) {
	super();
	this.packageName = packageName;
	this.domainName = domainName;
    }

    @Override
    public List<String> getImports() {
	String daoName = domainName + FILE_TYPE.DAO.getType();
	imports.add(AUTOWIRE);
	imports.add(SERVICE);
	imports.add(TRANSACTIONAL);
	imports.add(CrudService);
	imports.add(CommonUtils.packageFomatter(packageName,
	        FILE_TYPE.DAO) + CommonUtils.PKG_SPLITER + daoName);
	imports.add(packageName + CommonUtils.PKG_SPLITER + domainName);
	imports.add(packageName + CommonUtils.PKG_SPLITER + domainName + CommonUtils.BASE_EXAMPLE);
	return imports;
    }

    @Override
    public List<String> getHeaders() {
	headers.add(Annotation);
	headers.add(Annotation_1);
	return headers;
    }

    @Override
    public String getExtend() {
	extend = this.parseServiceExtends(domainName, CrudService);
	return extend;
    }

    @Override
    public List<String> getImplement() {
	return null;
    }

    @Override
    public List<String> getBody() {
	String daoName = domainName + FILE_TYPE.DAO.getType();
	body.add(Annotation_2);
	body.add("private " + daoName + " " + CommonUtils.lowerFirst(daoName) + ";");
	return body;
    }

    @Override
    public String getXMLName() {
	return null;
    }

    public String getPackageName() {
	return packageName;
    }

    public void setPackageName(String packageName) {
	this.packageName = packageName;
    }

    public String getDomainName() {
	return domainName;
    }

    public void setDomainName(String domainName) {
	this.domainName = domainName;
    }

    public void setImports(ArrayList<String> imports) {
	this.imports = imports;
    }

    public void setHeaders(ArrayList<String> headers) {
	this.headers = headers;
    }

    public void setExtend(String extend) {
	this.extend = extend;
    }

    public void setBody(ArrayList<String> body) {
	this.body = body;
    }

    private String parseServiceExtends(String domainName, String crudService) {
	StringBuilder sb = new StringBuilder("");
	sb.append(CommonUtils.getClzName(crudService) + "<" + domainName + CommonUtils.FILE_TYPE.DAO.getType() + ","
	        + domainName + "," + domainName + CommonUtils.BASE_EXAMPLE
	        + ">");
	return sb.toString();
    }

}
